package com.example.admin.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.admin.R;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.Payment;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.dao.feiyongDao;
import com.example.admin.SQLite.feiyong;
import com.example.admin.Utils.SimpleSpinner;
import com.example.admin.adapter.AdminTab1dapter;
import com.example.admin.adapter.AdminTab2dapter;
import com.example.admin.adapter.AdminUserdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Tab2Fragment extends Fragment {

    AdminTab2dapter userdapter;
    RecyclerView recyclerView;
    EditText s1, s2, s3, s4;
    feiyongDao feiyongDaos;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab2, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        s1 = view.findViewById(R.id.s1);
        s2 = view.findViewById(R.id.s2);
        s3 = view.findViewById(R.id.s3);
        s4 = view.findViewById(R.id.s4);

        feiyongDaos = new feiyongDao(getActivity());
        // ArrayList<Payment> list = dbHelper.getPayment();
        userdapter = new AdminTab2dapter();
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setAdapter(userdapter);
        userdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                feiyong bean = userdapter.getItem(position);
                if (bean.feiyongStatu.equals("未缴费")) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("是否执行此操作");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            feiyongDaos.updateCom(bean.id,"已缴费");
                            userdapter.getItem(position).feiyongStatu="已缴费";
                            userdapter.notifyItemChanged(position);
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();

                }
            }
        });
        // userdapter.setNewData(list);
        view.findViewById(R.id.tvCha).setOnClickListener(v -> {
            String s11 = s1.getText().toString().trim();
            String s21 = s2.getText().toString().trim();
            String s31 = s3.getText().toString().trim();
            String s41 = s4.getText().toString().trim();
            ArrayList<User> list1 = new ArrayList<>();
         /*   if (!TextUtils.isEmpty(s41)) {
                for (Payment user : list) {
                    if (user.userName.contains(s41)) {
                        list1.add(user);
                    }
                }
                userdapter.setNewData(list1);
            }*/
        });

        view.findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                View views = getLayoutInflater().inflate(R.layout.dialog_tab2, null);
                builder.setView(views);
                AlertDialog dialog = builder.create();
                dialog.show();
                ArrayList<String> list = new ArrayList<>();
                for (int i = 1; i < 100; i++) {
                    list.add(i + "幢");
                }
                ArrayList<String> list1 = new ArrayList<>();
                for (int i = 1; i < 3; i++) {
                    list1.add(i + "单元");
                }
                ArrayList<String> list2 = new ArrayList<>();
                for (int i = 1; i < 6; i++) {
                    list2.add("10" + i + "室");
                }
                SimpleSpinner spinner0 = views.findViewById(R.id.spinner0);
                SimpleSpinner spinner1 = views.findViewById(R.id.spinner1);
                spinner1.setContent(list);
                SimpleSpinner spinner2 = views.findViewById(R.id.spinner2);
                spinner2.setContent(list1);
                SimpleSpinner spinner3 = views.findViewById(R.id.spinner3);
                spinner3.setContent(list2);
                Spinner spinner4 = views.findViewById(R.id.spinner4);
                EditText tvCont = views.findViewById(R.id.tvCont);
                views.findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String s0 = (String) spinner0.getSelectedItem();
                        String s1 = (String) spinner1.getSelectedItem();
                        String s2 = (String) spinner2.getSelectedItem();
                        String s3 = (String) spinner3.getSelectedItem();
                        String s4 = (String) spinner4.getSelectedItem();
                        String tvConts = tvCont.getText().toString().trim();
                  /*      Payment bean = new Payment();
                        bean.Lou = s1;
                        bean.Dan = s2;
                        bean.Men = s3;
                        bean.type = s4;
                        bean.Uname = "--";
                        bean.statu = "未缴费";
                        bean.money = tvConts;
                        dbHelper.addPayment(bean);*/

                        feiyong beans = new feiyong();
                        beans.feiyongStatu = "未缴费";
                        beans.feiyongAmount = tvConts;
                        beans.feiyongShequ = s1 + s2 + s3;
                        beans.feiyongType = s4;
                        beans.feiyongHome = s0;
                        feiyongDaos.addData(beans);


                        getdata();
                        dialog.dismiss();
                    }
                });
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        getdata();
    }

    private void getdata() {
    /*    ArrayList<Payment> list = dbHelper.getPayment();
        userdapter.setNewData(list);*/
        ArrayList<feiyong> list = feiyongDaos.FindWhereData();
        userdapter.setNewData(list);
    }
}